
package MsgSystem;

import UserUnit.ImpPhoto;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
//ImpListener class process the packet attemps of the clients, in order to 
//activate the different methods depends of message types
public class ImpListener implements Listener, PacketListener, Serializable {

    private static final Log LOGGER = LogFactory.getLog("common");
    ArrayList results=null;
    ImpInstantMessage itm = null;
    ImpSearchProfile isp=null;
    final String GENERIC_MESSAGE = "GENERIC_MESSAGE";

    private enum Options {

        SEARCH_PROFILE, SEARCH_RESULTS, USER_CONTENT,}

    public ImpListener(ImpInstantMessage itm) {
        this.itm = itm;
    }

    public void processPacket(Packet packet) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Packet from: " + packet.getFrom() + " in decapsulates process");
        }

        if (packet instanceof Message) {

            Message msg = (Message) packet;
            String typeMsg = msg.getPacketID();

            if (typeMsg.equals("GENERIC_MESSAGE")) {


                ImpGenericMessage igm = (ImpGenericMessage) msg.getProperty(GENERIC_MESSAGE);


                String opt = igm.getMessageType();

                switch (ImpListener.Options.valueOf(opt)) {
                    case SEARCH_PROFILE:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Packet type: " + igm.getMessageType() + " from: " + packet.getFrom());
                        }
                        isp=igm.getSearchProfile();
                        results=itm.extract(isp);
                        ImpSearchResult isr = new ImpSearchResult(results, "SEARCH_RESULTS");
                        igm.setSearchResult(isr);

                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Packet type: " + igm.getMessageType() + " from: " + packet.getFrom() + " ready to send");
                        }
                        String comodin = igm.getDestination();
                        igm.setDestination(igm.getSource());
                        igm.setSource(comodin);
                        LOGGER.info("Message with results send to : " + igm.getDestination());
                        itm.sendMessage(igm.getDestination(), igm);

                        break;
                    case SEARCH_RESULTS:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Packet type: " + igm.getMessageType() + " from: " + packet.getFrom());
                        }
                           
                           isr = igm.getSearchResult();
                          
                        itm.passResults(isr);
        
                        break;

                    case USER_CONTENT:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Packet type: " + igm.getMessageType() + " from: " + packet.getFrom() + " ready to send");
                        }
                        String comodin2 = igm.getDestination();
                        igm.setDestination(igm.getSource());
                        igm.setSource(comodin2);
                        SendFilesClass sfc = new SendFilesClass(itm, igm);
                        LOGGER.info("Ready to activate the class to send a file");
                        sfc.send();
                        break;
                    default:
                        if (LOGGER.isDebugEnabled()) {
                            LOGGER.debug("Bad packet type: " + igm.getMessageType() + " from: " + packet.getFrom() + "\n Expected: SEARCH_PROFILE / SEARCH_RESULTS / USER_CONTENT");
                        } else {
                            LOGGER.fatal("PROGRAM FATAL ERROR! RESTART PLEASE!");
                        }
                        break;
                }
            }
        }



    }
}
